package proyectoia;

import IA.DistFS.Servers;
import aima.search.framework.GoalTest;
import aima.search.framework.HeuristicFunction;
import aima.search.framework.Search;
import aima.search.framework.SuccessorFunction;
import aima.search.informed.HillClimbingSearch;
import aima.search.informed.SimulatedAnnealingSearch;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.math3.stat.descriptive.SummaryStatistics;

public class StagedExperiment4 extends StagedExperiment {

    static int USERS = 0;
    static int SERVERS = 1;
    int current;
          
    
    public StagedExperiment4(int seed, int nIterations, int current) {
        super(seed, nIterations);
        numStages = 50;
        current = current;
    }
    
    
  
    @Override
    protected int getNumUsers() {
        if (current == USERS)
            return 100 + currentStage*100;
        else if (current == SERVERS) 
            return 200;
        return 200;
    }

    @Override
    protected int getNumRequests() {
        return 5;
    }

    @Override
    protected int getNumServers() {
        if (current == USERS)
            return 50 + currentStage*50;
        else if (current == SERVERS) 
            return 50;
        return 50;
    }

    @Override
    protected int getNumReplications() {
        return 5;
    }

    @Override
    protected DistFileSystemState getInitialSolution(DistFileSystemState state) {
        state.generateGreedySolution();
        return state;
    }

    @Override
    protected Search getSearch() {
        return new HillClimbingSearch();
    }

    @Override
    protected SuccessorFunction getSuccessor() {
        return new DistFileSystemSuccessorFunction2b();
    }

    @Override
    protected GoalTest getGoalTest() {
        return new DistFileSystemGoalTest();
    }

    @Override
    protected HeuristicFunction getHeuristic() {
        return new DistFileSystemHeuristicFunction1();
    }
    
    public void printSlowestServerTime(String filename) throws Exception {
        FileWriter outFile = new FileWriter("data/" + filename + "-slowest-server-time.data");
        PrintWriter o = new PrintWriter(outFile);
        for (int s = 0; s < numStages; s++) {
            SummaryStatistics rt = new SummaryStatistics();
            for (int i = 0; i < numIterations; i++) 
                rt.addValue(runTimes[s].get(i));
            o.println((int)rt.getMean() + "\t" + rt.getStandardDeviation() );
        }
        o.close();
    }
    
    public static void main(String[] args) throws Servers.WrongParametersException, Exception {

//        StagedExperiment4 se = new StagedExperiment4(123, 10, USERS);
//        se.initialitze();
//        se.execute();
//        se.printSlowestServerTime("experiment-4-users");
        
        StagedExperiment4 se2 = new StagedExperiment4(123, 10, SERVERS);
        se2.initialitze();
        se2.execute();
        se2.printSlowestServerTime("experiment-4-servers");
        
    }
}